﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz.Spi;
using Quartz.Net.Factory;
using Quartz.Net.Model;

namespace Quartz.Net.Core.Listener
{
   public class mySchedulerListener : ISchedulerListener
    {
        public void JobAdded(IJobDetail jobDetail)
        {
            //throw new NotImplementedException();
            Console.WriteLine(jobDetail.Key.Name +":Job添加");
            //factoryClass.InsertRrrorlog(jobDetail.Key.Name + ":Job添加", jobDetail.Key.Name + ":Job添加", LogsStatus.Fatal);
            List<Task_Quartz_JobTriggerModel> model = factoryClass.JobTriggerGetInfo().Where(m => m.VALID == "1" && m.JOBNAME == jobDetail.Key.Name).ToList();
            if (model.Count>0)
            {
                List<DateTime> list = QuartzHelper.GetNextFireTime(model[0].JOBCRON, 2);
                factoryClass.JobAdd(jobDetail.Key.Name, list);
            }
            
        }

        public void JobDeleted(JobKey jobKey)
        {
            //throw new NotImplementedException();
            Console.WriteLine(jobKey.Name+":Job删除");
            //factoryClass.JobDeleted(jobKey.Name);
        }

        public void JobPaused(JobKey jobKey)
        {
            //throw new NotImplementedException();
            Console.WriteLine(jobKey.Name + ":Job暂停");
            //factoryClass.JobPaused(jobKey.Name);
        }

        public void JobResumed(JobKey jobKey)
        {
            //throw new NotImplementedException();
            Console.WriteLine(jobKey.Name + ":Job继续");
            //factoryClass.JobResumed(jobKey.Name);
        }

        public void JobScheduled(ITrigger trigger)
        {
            //throw new NotImplementedException();
            Console.WriteLine(trigger.Key.Name+":Job加入Scheduled");
        }

        public void JobUnscheduled(TriggerKey triggerKey)
        {
            //throw new NotImplementedException();
            Console.WriteLine(triggerKey.Name+":Job计划外");
            factoryClass.InsertRrrorlog(triggerKey.Name + ":Job计划外", "Job计划外", LogsStatus.Fatal);
        }

        public void JobsPaused(string jobGroup)
        {
            //throw new NotImplementedException();
            Console.WriteLine("整个群组的Job暂停");
            factoryClass.InsertRrrorlog("整个群组的Job暂停", "整个群组的Job暂停", LogsStatus.Info);
        }

        public void JobsResumed(string jobGroup)
        {
            //throw new NotImplementedException();
            Console.WriteLine("整个群组的Job继续");
            factoryClass.InsertRrrorlog("整个群组的Job继续", "整个群组的Job继续", LogsStatus.Info);
        }

        public void SchedulerError(string msg, SchedulerException cause)
        {
            //throw new NotImplementedException();
            Console.WriteLine("SchedulerError");

            factoryClass.InsertRrrorlog(msg + "==========" + cause.StackTrace, msg, LogsStatus.Fatal);
        }

        public void SchedulerInStandbyMode()
        {
            //throw new NotImplementedException();
            Console.WriteLine("计划程序错误");
            factoryClass.InsertRrrorlog("计划程序错误", "计划程序错误", LogsStatus.Fatal);
        }

        public void SchedulerShutdown()
        {
            //throw new NotImplementedException();
            Console.WriteLine("调度程序关闭");
            factoryClass.DeleteRunAll();
            factoryClass.InsertRrrorlog("调度程序关闭", "调度程序关闭", LogsStatus.Fatal);
        }

        public void SchedulerShuttingdown()
        {
            //throw new NotImplementedException();
            Console.WriteLine("调度程序处理过程中关闭");
            factoryClass.DeleteRunAll();
            factoryClass.InsertRrrorlog("调度程序处理过程中关闭", "调度程序处理过程中关闭", LogsStatus.Fatal);
        }

        public void SchedulerStarted()
        {
            //throw new NotImplementedException();
            Console.WriteLine("调度程序已启动");
            factoryClass.InsertRrrorlog("调度程序已启动", "调度程序已启动", LogsStatus.Info);
        }

        public void SchedulerStarting()
        {
            //throw new NotImplementedException();
            Console.WriteLine("调度程序正在启动");
            factoryClass.InsertRrrorlog("调度程序正在启动", "调度程序正在启动", LogsStatus.Info);
        }

        public void SchedulingDataCleared()
        {
            //throw new NotImplementedException();
            Console.WriteLine("SchedulingDataCleared");
            factoryClass.InsertRrrorlog("SchedulingDataCleared", "SchedulingDataCleared", LogsStatus.Info);
        }

        public void TriggerFinalized(ITrigger trigger)
        {
            //throw new NotImplementedException();
            Console.WriteLine("TriggerFinalized");
        }

        public void TriggerPaused(TriggerKey triggerKey)
        {
            //throw new NotImplementedException();
            Console.WriteLine("TriggerPaused");
        }

        public void TriggerResumed(TriggerKey triggerKey)
        {
            //throw new NotImplementedException();
            Console.WriteLine("TriggerResumed");
        }

        public void TriggersPaused(string triggerGroup)
        {
            //throw new NotImplementedException();
            Console.WriteLine("TriggersPaused");
        }

        public void TriggersResumed(string triggerGroup)
        {
            //throw new NotImplementedException();
            Console.WriteLine("TriggersResumed");
        }
    }
}
